package com.yiwei.zxp.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BoothPacketActivity;
import com.yiwei.zxp.activity.MyOrderActivity;
import com.yiwei.zxp.activity.UlinePayFinishActivity;
import com.yiwei.zxp.adapter.MyOrderAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ComboBindCardBean;
import com.yiwei.zxp.api.bean.MyOrderListBean;
import com.yiwei.zxp.api.bean.OrderDetailBean;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-4-10.
 */

public class MyOrderFragment extends BaseFragment implements MyOrderAdapter.OnChooseListener {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private int id;
    //    private static final String STATUS_ALL = "0";
//    private static final String STATUS_UNPAY = "1";
//    private static final String STATUS_SEND = "2";
//    private static final String STATUS_WAIT_RECEIVE = "3";
//    private static final String STATUS_PINGJIA = "4";
//    private String selectStatus;
    //查询状态（该状态不是订单真实状态），不填或填空或填0：全部订单，1：为未付款，2：为已付款、分期中、已完成
    private MyOrderAdapter adapter;
    private OnLoadeMoreListener onLoadeMoreListener;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_order;
    }

    //    "全部", "待付款", "已付款"
    @Override
    protected void initView() {
        if (getArguments() != null) {
            id = getArguments().getInt("id", 0);
        }

        onLoadeMoreListener = new OnLoadeMoreListener() {
            @Override
            public void onLoadMore(int page) {
                getOrderList(page, id + "");
            }
        };
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage(1);
                getOrderList(1, id + "");
            }
        });
        list.addOnScrollListener(onLoadeMoreListener);
        adapter = new MyOrderAdapter(getContext());
        adapter.setOnChooseListener(this);
        list.setAdapter(adapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        onLoadeMoreListener.setPage(1);
        getOrderList(1, id + "");
    }

    public void getOrderList(int page, String status) {
        //    "全部", "待付款", "已付款"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
        MyOrderListBean bean = new MyOrderListBean();
        bean.status = status;
        bean.page = page + "";
        Api.api_service.getOrderList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }

                });
    }

    public void getOrderDelete(String order_num) {
        //    "全部", "待付款", "已付款"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
        OrderDetailBean bean = new OrderDetailBean();
        bean.order_number = order_num;
        Api.api_service.getOrderDelete(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(getContext(), "删除成功");
                    onLoadeMoreListener.setPage(1);
                    getOrderList(1, id + "");
                });
    }

    public void getBindBank(String order_num, String bank_num) {
        //    "全部", "待付款", "已付款"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
        ComboBindCardBean bean = new ComboBindCardBean();
        bean.bankcard_no = bank_num;
        bean.order_number = order_num;
        Api.api_service.getComboBindCard(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Dialog.showDefaultDialog(getContext(), "", "待平台核对银行卡张号，请保持手机号码通畅！", "", "好", false, new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {
                            onLoadeMoreListener.setPage(1);
                            getOrderList(1, id + "");
                        }

                        @Override
                        public void cancel() {

                        }
                    });

                });
    }


    @Override
    public void onClick(String money, String order_no) {
//        ((MyOrderActivity) getActivity()).rePay(Double.valueOf(money), order_no);
    }

    @Override
    public void onDelete(String money, String order_no) {
        Dialog.showDefaultDialog(getContext(), "", "是否确定删除该订单？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                getOrderDelete(order_no);
            }

            @Override
            public void cancel() {

            }
        });

    }

    @Override
    public void onBuyMoreTime() {
        Util.startActivity(getActivity(), BoothPacketActivity.class);
        getActivity().finish();
    }

    @Override
    public void onPayFinish(String order_no) {
        Intent intent = new Intent(getContext(), UlinePayFinishActivity.class);
        intent.putExtra("order_number", order_no);
        intent.putExtra("is_has_upload", false);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }
}
